#!/bin/bash
#chkconfig: 345 20 80
# description: logstashservice daemon
# processname: logstashservice
# logstashservice daemon

function startLogstashDaemon {
	current_process=$(ps -ef |grep logstash |grep -v grep | grep <%=@logstash=%> | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		echo "...logstash service daemon is already running"
	else 
		echo "...starting logstash service daemon"
		export JAVA_HOME=<%=@java_home=%>;
		nohup <%=@logstash_dir=%>/bin/logstash -f <%=@logstash_dir=%>/shipper.conf 2>/dev/null >  /dev/null   & 
	fi
}

function statusLogstashDaemon {
	current_process=$(ps -ef |grep logstash |grep -v grep| grep <%=@logstash=%> | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		echo "...logstash service daemon: [RUNNING]"
	else
		echo "...logstash service daemon: [STOPPED]"
	fi
}

function stopLogstashDaemon {
	current_process=$(ps -ef |grep logstash |grep -v grep | grep <%=@logstash=%> | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		echo "...stopping processes for logstash service daemon"
		kill -9 $current_process
	else
		echo "...logstash service daemon not running"
	fi
}


function usage {
	echo "Usage $0 {status|start|stop|restart}"
}

case "$1" in 
	start) 
		startLogstashDaemon 
		statusLogstashDaemon
	;;
	status) 
		statusLogstashDaemon
		statusLogstashDaemon
	;;
	stop)
		stopLogstashDaemon
		statusLogstashDaemon
	;;
	restart)
		stopLogstashDaemon
		startLogstashDaemon
		statusLogstashDaemon
	;;
	*)
		usage
	;;
esac






